package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.goldtreasure.views.WithoutEmojiEditext;

public class FeedBackActivity extends BaseActivity {
    private TitleView mTitleView;
    private Button btnSubmit;
    private WithoutEmojiEditext etFeedBack;
    private TextView tvDialNum;
    private TextView mHasInputNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();

    }

    private void initView() {
        mTitleView = (TitleView) findViewById(R.id.title_view);
        mTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleView.getTitletextView().setText("意见反馈");
        mTitleView.getBackView().setVisibility(View.VISIBLE);
        mTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mHasInputNum = (TextView) findViewById(R.id.has_input_num);
        etFeedBack = (WithoutEmojiEditext) findViewById(R.id.et_feedback);
        etFeedBack.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mHasInputNum.setText(s.toString().trim().length() + "");
            }
        });
        tvDialNum = (TextView) findViewById(R.id.dial_num);
        tvDialNum.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                UIHelper.showCallRemindDialog(FeedBackActivity.this, tvDialNum.getText().toString());
            }
        });
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String feedback = etFeedBack.getText().toString().trim();
                if (TextUtils.isEmpty(feedback)) {
                    UIHelper.showToast(FeedBackActivity.this, "请输入反馈内容!");
                    return;
                }
                submitFeedback(feedback);
            }

        });
    }

    protected void submitFeedback(String feedback) {
        ActionUtil actionUtil = new ActionUtil(FeedBackActivity.this);
        actionUtil.submitFeedBack(feedback);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                if (model == null) {
                    UIHelper.showToast(FeedBackActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (model.getErrorMsg() != null) {
                    UIHelper.showToast(FeedBackActivity.this, model.getErrorMsg());
                } else {
                    UIHelper.showToast(FeedBackActivity.this, model.getInfo());
                    finish();
                }
            }
        });
    }
}
